% Mass statistical test that compares 3 methods of template matching:
% - Kunchenko's polynomials
% - Signal cross-correlation
% - Sum of squared differences squares
%
% template and signal initializing
step = 0.01;
% template - canonacal gaussian
[xpsi psi] = template3(step);
% cell-array of generated functions
generatedFunctions{1} = @(x)1;
generatedFunctions{2} = @(x)x;
generatedFunctions{3} = @(x)x.^2;
generatedFunctions{4} = @(x)x.^3;
generatedFunctions{5} = @(x)x.^4;
generatedFunctions{6} = @(x)x.^5;
generatedFunctions{7} = @(x)x.^6;
% trust level to Kunchenko and Cross-correlation method
KunchenkoCorrelationTrustLevel = .4:.1:.9;
% maximum derivation in
trustedDerivation = 0.1;
% number of gaussians in experiments
gaussiansNumber = 10;
% S - number of statistic iterations (size of samlpe)
S = 10;

% ================= clean signal matching =================================

% effectivity results initialization

KunchenkoFindedGaussiansNumber = zeros(1, length(KunchenkoCorrelationTrustLevel));
KunchenkoWrongGaussiansNumber = zeros(1, length(KunchenkoCorrelationTrustLevel));

for i = 1: length(KunchenkoCorrelationTrustLevel)
    for count = 1:S
        % statistic experiment
        % signal generating - sum of gaussians that are generated randomly
        [x sign means fmeans] = signal8(step, gaussiansNumber);
        % getting maximums of clean signal
        [xSignMax SignMax] = maxEdges(x, sign, .8 * min(fmeans));
        
        
        % ===== Kunchenko's polynomials ======================================
        [~, ~, e] = Kunchenko2(sign, psi, step, generatedFunctions);
        scaledKunchenkoEffectogram = KunchenkoEffectogramScaling(x, e, xpsi);
        % getting maximums of thresholded effectogram that are over trust level
        [trustedMaximums ~] = maxEdges(x, scaledKunchenkoEffectogram, KunchenkoCorrelationTrustLevel(i));
        % getting number of finded gaussians
        numberFindedGaussians = searchEffectivity(xSignMax, trustedDerivation, trustedMaximums);
        KunchenkoFindedGaussiansNumber(i) = KunchenkoFindedGaussiansNumber(i) + numberFindedGaussians;
        % getting number of wrong-found gaussians
        KunchenkoWrongGaussiansNumber(i) = KunchenkoWrongGaussiansNumber(i)...
            + (length(trustedMaximums) - numberFindedGaussians);        
        
    end;
end;

% getting average values
KunchenkoFindedGaussiansNumber = KunchenkoFindedGaussiansNumber / S;
KunchenkoWrongGaussiansNumber = KunchenkoWrongGaussiansNumber / S;

% plotting results
figure;
plot(KunchenkoCorrelationTrustLevel, KunchenkoFindedGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Kunchenko''s polynomials');
xlabel('Trust level to Kunchenko');
ylabel('Number of found gaussians');

figure;
plot(KunchenkoCorrelationTrustLevel, KunchenkoWrongGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Kunchenko''s polynomials');
xlabel('Trust level to Kunchenko');
ylabel('Number of wrong-found gaussians');